# Copyright (c) 2018 Presto Labs Pte. Ltd.
# Author: xguo

import logging

from coin.exchange.base.order_gateway import OrderType
from coin.exchange.hitbtc_v2.kr_rest.product import HitbtcProduct
from coin.exchange.hitbtc_v2.order_gateway import HitbtcOrderGateway
from coin.strategy.executor.passive.simple_mm_executor import \
  SimpleMMExecutor
from coin.strategy.executor.util.traits import (order_logger, SpotExecutorTraits)


class HitbtcSimpleTraits(SpotExecutorTraits):
  def __init__(self, executor_config, order_gateway, logger=None):
    logger = logger or logging.getLogger('HitbtcSimpleMMExecutor')
    super().__init__(executor_config, order_gateway, logger)

  @order_logger
  def _submit_order(self, product, price, qty, order_side):
    if not self.og.submittable():
      self._logger.warning('Skip submission: %s %f@%f' % (product, qty, price))
      return False

    self.og.submit(product, price, qty, order_side, OrderType.LIMIT)

  @order_logger
  def _submit_pass_order(self, product, price, qty, order_side):
    if not self.og.submittable():
      self._logger.warning('Skip submission: %s %f@%f' % (product, qty, price))
      return False

    self.og.submit(product,
                   price,
                   qty,
                   order_side,
                   OrderType.LIMIT,
                   post_only=self.config.post_only)

  def _cancel_orders_impl(self, order_id_list):
    self.og.cancel_multiple(order_id_list)


class HitbtcSimpleMMExecutor(SimpleMMExecutor):
  ProductType = HitbtcProduct
  OGType = HitbtcOrderGateway
  Traits = HitbtcSimpleTraits
